<template>
  <div class="div_box">
    <div class="div_top">
      <a href="/about">去about吧</a>
    </div>
    <div class="div_center" @scroll="onScroll">
      <ul class="ul_box">
        <li v-for="(item, index) in list" :key="index">
          <img :src="item.productImage" />
          <p>{{ item.productName }}</p>
          <p>￥：{{ item.salePrice }}.00</p>
          <button>加入购物车</button>
        </li>
      </ul>
    </div>
    <div class="div_bottom" v-show="isLoading">
      <img src="../../public/loading.gif" />
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [],
      isLoading: false, //让loading消失
    };
  },
  mounted() {
    this.$axios.get("/data.json").then((res) => {
      console.log(res.data.result.list);
      this.list = res.data.result.list;
    });
  },
  methods: {
    onScroll(event) {
      console.log(event);
      //获取可视区域的高度
      var clientHeight = event.target.clientHeight;
      console.log(clientHeight);
      //获取滚动视图的高度
      var scrollHeight = event.target.scrollHeight;
      console.log(scrollHeight);
      //获取滚动视图的滚动距离
      var scrollToTop = parseInt(event.target.scrollTop);
      console.log(scrollToTop);

      if (scrollHeight - scrollToTop - clientHeight <=1) {
        this.isLoading = true;
        this.$axios.get("/data2.json").then((res) => {
          console.log(res);
          setTimeout(() => {
            // this.list.push(res.data.result.list)
            res.data.result.list.forEach((item) => {
              this.list.push(item);
              this.isLoading = false;
            });
          }, 3000);
        });
      }
    },
  },
};
</script>

<style lang="scss">
.div_box {
  width: 800px;
  margin: 100px auto;
}
.ul_box {
  width: 100%;
  overflow: scroll;
  display: flex;
  justify-content: space-around;
  flex-wrap: wrap;
  align-items: center;
  text-align: center;
  li {
    width: 23%;
    height: 20rem;
    border: 1px solid lightblue;
    margin: 0.4rem;
    box-shadow: 2px 2px 2px gainsboro;
    &:hover{
      border: 1px solid lightpink;
      transition: all;
    }
    img {
      width: 10rem;
      height: 12rem;
    }
    p {
      margin-top: 0.5rem;
      &:nth-child(3) {
        margin-top: 0.5rem;
        color: red;
      }
    }
    button {
      width: 95%;
      height: 2.5rem;
      margin-top: 1rem;
      border: 1px solid lightcoral;
      color: lightcoral;
    }
  }
}
.div_top {
  width: 100%;
  height: 10vh;
  background: lightcyan;
}
.div_center {
  width: 100%;
  height: 80vh;
  overflow: scroll;
}
.div_bottom {
  width: 100%;
  height: 10vh;
  background: lightcyan;
  img {
    width: 10rem;
    margin-left: 20rem;
  }
}
</style>